#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define all(x) (x).begin(),(x).end()
typedef long long ll;

typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<VI> VVI;

typedef pair<int,int> PII;
typedef vector<PII> VPII;
#define X first
#define Y second
#define mp make_pair

#define two(x) (1<<(x))
#define twoll(x) ((long long)1<<(x))
#define contain(s,x) (((s)&two(x))!=0)
#define containll(s,x) (((s)&twoll(x))!=0)

#define db(a) cout << #a << "=" << a << " "
#define dbn cout << endl
#define sqr(x) (x)*(x)

string STR(int n) { char tmp[20]; sprintf(tmp,"%d",n); return tmp; }
int INT(string s) { return atoi(s.c_str()); }
template <class Ty, class Tx> Ty to(const Tx &x) { Ty y; stringstream ss; ss<<x; ss>>y; return y; }

const int INF = 1000000007;
const double EPS = 1e-10;

char buf[1<<20];
int TC;

int accum[24*3600];
bool isThree[24*3600];

//struct Time {
//	int h,m,s;
//	Time(int H, int M, int S) : h(H), m(M), s(S) {}
//	void add() {
//		s++;
//		if (s == 60)
//			s = 0, m++;
//		if (m == 60)
//			m = 0, h++;
//		if (h == 24)
//			h = 0;
//	}
//	bool three() {
//		return (h*10000 + m*100 + s)%3 == 0;
//	}
//	bool equal(Time &t) const {
//		return h == t.h && m == t.m && s == t.s;
//	}
//};

bool check(int n) {
	int h = n/3600;
	int m = n%3600/60;
	int s = n%60;
	return (h*10000 + m*100 + s)%3 == 0;
}

const int mod = 24*3600;

int main() {
#define SUBMIT 1
#if !SUBMIT
	freopen("DATA.in", "rt", stdin);
	freopen("DATA.ou", "wt", stdout);
#endif

	gets(buf); TC = atoi(buf);
	
	memset(accum,0,sizeof(accum));
	memset(isThree,0,sizeof(isThree));
	int acc = 0;
	for (int i = 0; i < 24*3600; ++i)
		if (check(i))
			accum[i] = acc++, isThree[i] = 1;
		else
			accum[i] = acc;
	
	while (TC--) {
		int h1,m1,s1,h2,m2,s2;
		scanf("%d:%d:%d %d:%d:%d",&h1,&m1,&s1,&h2,&m2,&s2);
		int res = 0;
		int t1 = h1*3600 + m1*60 + s1;
		int t2 = h2*3600 + m2*60 + s2;
		if (t1 == t2)
			res = isThree[t1];
		else if (t1 < t2)
			res = accum[t2]-accum[t1]+isThree[t2];
		else
			res = accum[mod-1]+isThree[mod-1]-(accum[t1]-accum[t2]-isThree[t2]);
		printf("%d\n",res);
	}

	return 0;
}





